import java.util.*;
import java.io.*;

class FileAnalyze {
	public static void main(String[] args) {
		if(args.length != 2) {
			System.out.println("Usage: FileAnalyze <filename> <fast|slow>");
			System.exit(0);
		}
		String filename = args[0];
		String method = args[1];
		try {
			InputStream input = new FileInputStream(filename);
			if(method.equals("fast"))
				input = new BufferedInputStream(input);
			
			int c = input.read();
			long start = System.currentTimeMillis();
			int total = 0;
			
			/*
			 * Accumulates the frequency of the individual characters
			 * in a hashmap.
			 */
			Map<Integer, Integer> counts = new TreeMap<Integer, Integer>();
			while(c >= 0) {
				total += 1;
				if(counts.containsKey(c)) {
					int count = counts.get(c);
					counts.put(c, count + 1);
				} else {
					counts.put(c, 1);
				}
				c = input.read();
			}
			long duration = (System.currentTimeMillis() - start);
			
			/*
			 * Print out the frequency of the individual characters
			 */
			List<Integer> allChar = new Vector<Integer>(counts.keySet());
			Collections.sort(allChar);
			for(int ch : allChar) {
				System.out.println((char)ch + ": " + counts.get(ch));
			}
			System.out.println("There are " + total + " characters in " + duration + " ms.");
			input.close();
		} catch(IOException e) {
			System.out.println("Error: " + e);
		}
	}
}
